UNPKG

co-share

Version:

A Javascript framework for easily building shared applications such as chats and games

89 lines 4.22 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.RootStore = exports.SubscribeRequest = void 0; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var _1 = require("."); var subscriber_1 = require("./subscriber"); exports.SubscribeRequest = _1.Request.createUnbound("requestSubscribe", function (origin, id, path) { if (origin == null) { throw "subscribeToChild can only be executed remotely using \"publish\""; } var store = this.storeMap.get(path); if (store == null) { return (0, rxjs_1.throwError)("unable to find store with path \"".concat(path, "\"")); } if (store instanceof store.subscriber.storeClass) { return new rxjs_1.Observable(function (subscriber) { return store.subscriber(origin.connection, function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } return subscriber.next(params); }, function (reason) { return subscriber.error(reason); }); }).pipe((0, operators_1.tap)(function () { return store.link(id, origin.connection); })); } else { return (0, rxjs_1.throwError)("Subscribed store has no correct implemented subscriber. Subscriber must be created with the store class."); } }); var RootStore = /** @class */ (function (_super) { __extends(RootStore, _super); function RootStore(storeMap) { var _this = _super.call(this) || this; _this.storeMap = storeMap; _this.subscriber = subscriber_1.Subscriber.create(RootStore, function (connection, accept, deny) { return deny("can't subscribe to the root store"); }); _this.requestSubscribe = exports.SubscribeRequest.bindTo(_this); return _this; } RootStore.prototype.addStore = function (store, path) { if (this.storeMap.has(path)) { throw "a store with the path \"".concat(path, " was already added, a store's path must be unique inside one root store\""); } this.storeMap.set(path, store); }; RootStore.prototype.destroyStore = function (store, path) { var s = this.storeMap.get(path); if (s == null) { console.warn("no store on path \"".concat(path, "\" found")); return; } if (s === store) { this.storeMap.delete(path); store.linkSet.forEach(function (link) { return link.close(); }); return; } throw "the provided store with path \"".concat(path, "\" is occupied by another store and therefore can't be destroyed"); }; RootStore.prototype.subscribe = function (path, parentLink, storeFactory) { var _this = this; var newLinkId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); return this.requestSubscribe.publishTo(parentLink, newLinkId, path).pipe((0, operators_1.map)(function (remoteParams) { var store = storeFactory.apply(void 0, remoteParams); _this.addStore(store, path); var link = store.link(newLinkId, parentLink.connection); return [store, link]; })); }; RootStore.prototype.onUnlink = function (link) { }; RootStore.prototype.onLink = function (link) { }; return RootStore; }(_1.Store)); exports.RootStore = RootStore; //# sourceMappingURL=root-store.js.map