mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-20 23:48:05 +03:00
636f81703c
Also: Add an Environment spec helper class that swaps out global variables to simulate running code in different environments for the replication specs.
59 lines
1.6 KiB
CoffeeScript
59 lines
1.6 KiB
CoffeeScript
{Site} = require 'telepath'
|
|
fsUtils = require 'fs-utils'
|
|
Project = require 'project'
|
|
|
|
module.exports =
|
|
class Environment
|
|
constructor: ({@site, @state, siteId, projectPath}={}) ->
|
|
@site ?= new Site(siteId ? 1)
|
|
if @state?
|
|
@run => @project = deserialize(@state.get('project'))
|
|
else
|
|
@state = @site.createDocument({})
|
|
@project = new Project(projectPath ? fsUtils.resolveOnLoadPath('fixtures'))
|
|
@state.set(project: @project.getState())
|
|
|
|
clone: (params) ->
|
|
site = new Site(params.siteId)
|
|
new Environment(site: site, state: @state.clone(site))
|
|
|
|
destroy: ->
|
|
@project.destroy()
|
|
|
|
getState: -> @state
|
|
|
|
run: (fn) ->
|
|
uninstall = @install()
|
|
fn()
|
|
uninstall()
|
|
|
|
install: ->
|
|
oldSite = window.site
|
|
oldProject = window.project
|
|
window.site = @site
|
|
window.project = @project
|
|
->
|
|
window.site = oldSite
|
|
window.project = oldProject
|
|
|
|
connect: (otherEnv) ->
|
|
new EnvironmentConnection(this, otherEnv)
|
|
|
|
|
|
connectDocuments: (docA, docB, envB) ->
|
|
|
|
class EnvironmentConnection
|
|
constructor: (@envA, @envB) ->
|
|
@envA.getState().connect(@envB.getState())
|
|
|
|
connect: (docA, docB) ->
|
|
unless docA.site is @envA.site
|
|
throw new Error("Document and environment sites do not match (doc: site #{docA.site.id}, env: site #{@envA.site.id})")
|
|
unless docB.site is @envB.site
|
|
throw new Error("Document and environment sites do not match (doc: site #{docB.site.id}, env: site #{@envB.site.id})")
|
|
|
|
connection = docA.connect(docB)
|
|
connection.abFilter = (fn) => @envB.run(fn)
|
|
connection.baFilter = (fn) => @envA.run(fn)
|
|
connection
|