UNPKG

wechaty-redux

Version:

Wechaty Redux Plugin Powered By Ducks

114 lines 4.43 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.puppetRef = exports.decreasePuppetReferenceInRegistry = exports.increasePuppetReferenceInRegistry = exports.registerPuppetInRegistry = void 0; const rxjs_1 = require("rxjs"); const duck = __importStar(require("../duck/mod.js")); const puppetRef = new Map(); exports.puppetRef = puppetRef; const increasePuppetReferenceInRegistry = (registry) => (puppet) => { const counter = puppetRef.get(puppet.id) ?? 0; const incCounter = counter + 1; if (incCounter === 1) { registry.set(puppet.id, puppet); } puppetRef.set(puppet.id, incCounter); return incCounter; }; exports.increasePuppetReferenceInRegistry = increasePuppetReferenceInRegistry; const decreasePuppetReferenceInRegistry = (registry) => (puppet) => { const counter = puppetRef.get(puppet.id) ?? 0; const decCounter = counter - 1; puppetRef.set(puppet.id, decCounter); if (decCounter <= 0) { registry.delete(puppet.id); puppetRef.delete(puppet.id); } return decCounter; }; exports.decreasePuppetReferenceInRegistry = decreasePuppetReferenceInRegistry; /** * Puppet will be automatic registered/deregistered inside the RxJS operator * * - Creating new operators from scratch * @see https://rxjs.dev/guide/operators * */ const registerPuppetInRegistry = (registry) => (puppet, options) => (observable) => new rxjs_1.Observable(subscriber => { /** * For emitting the `RregisterPuppet` action */ const proxySubject = new rxjs_1.Subject(); /** * Chain the subscription to the observable */ const proxySubscription = observable.subscribe(proxySubject); const finalSubscription = proxySubject.subscribe(subscriber); const counter = increasePuppetReferenceInRegistry(registry)(puppet); /** * Emit `RegisterPuppet` action when first time subscribe to the puppet */ if (counter === 1) { proxySubject.next(duck.actions.REGISTER_PUPPET_COMMAND(puppet.id)); if (options?.store && options.wechaty) { options.store.dispatch(duck.actions.BIND_WECHATY_PUPPET_COMMAND({ puppetId: puppet.id, wechatyId: options.wechaty.id, })); } } /** * Return the teardown logic. * * This will be invoked when the result errors, completes, or is unsubscribed. */ return () => { finalSubscription.unsubscribe(); proxySubscription.unsubscribe(); /** * Cleanup puppet in registry with reference counter */ const counter = decreasePuppetReferenceInRegistry(registry)(puppet); if (counter <= 0) { if (options?.store) { /** * Unbind Wechaty <> Puppet */ if (options.wechaty) { options.store.dispatch(duck.actions.UNBIND_WECHATY_PUPPET_COMMAND({ puppetId: puppet.id, wechatyId: options.wechaty.id, })); } /** * Deregister Puppet */ options.store.dispatch(duck.actions.DEREGISTER_PUPPET_COMMAND(puppet.id)); } } }; }); exports.registerPuppetInRegistry = registerPuppetInRegistry; //# sourceMappingURL=register-puppet.js.map