@shaxpir/duiduidui-models 1.3.3 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ import { Doc } from '@shaxpir/sharedb/lib/client';
2
+ import { ShareSync } from '../repo';
3
+ import { Content, ContentBody, ContentId, ContentMeta } from "./Content";
4
+ export interface BillingPayload {
5
+ stripe_customer_id: string;
6
+ }
7
+ export interface BillingBody extends ContentBody {
8
+ meta: ContentMeta;
9
+ payload: BillingPayload;
10
+ }
11
+ export declare class Billing extends Content {
12
+ static makeBillingId(userId: ContentId): ContentId;
13
+ static create(userId: ContentId): Billing;
14
+ constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
15
+ get payload(): BillingPayload;
16
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Billing = void 0;
4
+ const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
5
+ const repo_1 = require("../repo");
6
+ const Content_1 = require("./Content");
7
+ const ContentKind_1 = require("./ContentKind");
8
+ class Billing extends Content_1.Content {
9
+ static makeBillingId(userId) {
10
+ return shaxpir_common_1.CachingHasher.makeMd5Base62Hash(userId + "-" + ContentKind_1.ContentKind.BILLING);
11
+ }
12
+ static create(userId) {
13
+ const now = shaxpir_common_1.MultiClock.now();
14
+ const billingId = Billing.makeBillingId(userId);
15
+ return repo_1.ShareSyncFactory.get().createContent({
16
+ meta: {
17
+ ref: billingId,
18
+ kind: ContentKind_1.ContentKind.BILLING,
19
+ id: billingId,
20
+ owner: userId,
21
+ created_at: now,
22
+ updated_at: now
23
+ },
24
+ payload: {
25
+ stripe_customer_id: null // Initially null, set when the user subscribes
26
+ }
27
+ });
28
+ }
29
+ constructor(doc, shouldAcquire, shareSync) {
30
+ super(doc, shouldAcquire, shareSync);
31
+ }
32
+ get payload() {
33
+ this.checkDisposed("Billing.payload");
34
+ return this.doc.data.payload;
35
+ }
36
+ }
37
+ exports.Billing = Billing;
@@ -1,6 +1,7 @@
1
1
  import { Doc } from '@shaxpir/sharedb/lib/client';
2
2
  import { CompactDateTime, MultiTime } from '@shaxpir/shaxpir-common';
3
3
  import { ShareSync } from '../repo';
4
+ import { BillingPayload } from './Billing';
4
5
  import { ContentKind } from './ContentKind';
5
6
  import { DevicePayload } from './Device';
6
7
  import { MediaPayload } from './Media';
@@ -35,7 +36,7 @@ export interface ContentMeta {
35
36
  created_at: MultiTime;
36
37
  updated_at: MultiTime;
37
38
  }
38
- export type ContentPayload = DevicePayload | MediaPayload | MetricPayload | ProfilePayload | ProgressPayload | SessionPayload | TermPayload | UserPayload | WorkspacePayload;
39
+ export type ContentPayload = BillingPayload | DevicePayload | MediaPayload | MetricPayload | ProfilePayload | ProgressPayload | SessionPayload | TermPayload | UserPayload | WorkspacePayload;
39
40
  export declare abstract class Content extends Model {
40
41
  static ID_LENGTH: number;
41
42
  constructor(doc: Doc, shouldAcquire: boolean, shareSync: ShareSync);
@@ -12,6 +12,8 @@ export declare abstract class Model {
12
12
  private _isForbidden;
13
13
  private _shouldPerformModelChangeAnalysis;
14
14
  private _dataBeforeOpBatch;
15
+ private _hasEventListeners;
16
+ private _setupEventListeners;
15
17
  private _clearEventListeners;
16
18
  protected shareSync: ShareSync;
17
19
  private _subscribingPromise;
@@ -8,6 +8,7 @@ const ChangeModel_1 = require("./ChangeModel");
8
8
  class Model {
9
9
  constructor(doc, shouldAcquire, shareSync) {
10
10
  this._dataBeforeOpBatch = null;
11
+ this._setupEventListeners = null;
11
12
  this._clearEventListeners = null;
12
13
  this._subscribingPromise = null;
13
14
  this._acquireCount = 0;
@@ -16,6 +17,7 @@ class Model {
16
17
  model.shareSync = shareSync;
17
18
  model._isDisposed = false;
18
19
  model._isForbidden = false;
20
+ model._hasEventListeners = false;
19
21
  // When a model is newly created, or retrieved via a "fetch query", treat that as an implicit
20
22
  // call to "acquire", since the SharedDB doc object will eventually need to be disposed.
21
23
  if (shouldAcquire) {
@@ -46,12 +48,18 @@ class Model {
46
48
  shaxpir_common_1.Dispatch.publish(Model.CHANGED, change);
47
49
  }
48
50
  };
49
- model.doc.on('before op batch', onBeforeOpBatch);
50
- model.doc.on('op batch', onOpBatch);
51
+ this._setupEventListeners = function () {
52
+ if (model.doc && model.doc.on) {
53
+ model.doc.on('before op batch', onBeforeOpBatch);
54
+ model.doc.on('op batch', onOpBatch);
55
+ model._hasEventListeners = true;
56
+ }
57
+ };
51
58
  this._clearEventListeners = function () {
52
59
  if (model.doc && model.doc.off) {
53
60
  model.doc.off('before op batch', onBeforeOpBatch);
54
61
  model.doc.off('op batch', onOpBatch);
62
+ model._hasEventListeners = false;
55
63
  }
56
64
  };
57
65
  }
@@ -78,6 +86,9 @@ class Model {
78
86
  }
79
87
  async acquire(minVersion) {
80
88
  this._acquireCount++;
89
+ if (!this._hasEventListeners) {
90
+ this._setupEventListeners();
91
+ }
81
92
  if (minVersion) {
82
93
  this.log(`ACQUIRE: (${this.compoundKey}) at minVersion '${minVersion}'; acquireCount = ${this.acquireCount}`);
83
94
  return this.ensureRecentData(minVersion);
@@ -41,10 +41,12 @@ const client_1 = __importStar(require("@shaxpir/sharedb/lib/client"));
41
41
  const shaxpir_common_1 = require("@shaxpir/shaxpir-common");
42
42
  const reconnecting_websocket_1 = __importDefault(require("reconnecting-websocket"));
43
43
  const models_1 = require("../models");
44
+ const Billing_1 = require("../models/Billing");
44
45
  const Content_1 = require("../models/Content");
45
46
  const ContentKind_1 = require("../models/ContentKind");
46
47
  const Device_1 = require("../models/Device");
47
48
  const Manifest_1 = require("../models/Manifest");
49
+ const Session_1 = require("../models/Session");
48
50
  const User_1 = require("../models/User");
49
51
  // Register the ShareDB types
50
52
  const Json1 = __importStar(require("./PermissiveJson1"));
@@ -335,18 +337,39 @@ class ShareSync {
335
337
  }
336
338
  wrap(kind, doc, shouldAcquire) {
337
339
  const shareSync = this;
338
- if (kind === ContentKind_1.ContentKind.MANIFEST) {
339
- return new Manifest_1.Manifest(doc, shouldAcquire, shareSync);
340
- }
341
- else if (kind === ContentKind_1.ContentKind.USER) {
342
- return new User_1.User(doc, shouldAcquire, shareSync);
340
+ if (kind === ContentKind_1.ContentKind.BILLING) {
341
+ return new Billing_1.Billing(doc, shouldAcquire, shareSync);
343
342
  }
344
343
  else if (kind === ContentKind_1.ContentKind.DEVICE) {
345
344
  return new Device_1.Device(doc, shouldAcquire, shareSync);
346
345
  }
346
+ else if (kind === ContentKind_1.ContentKind.MANIFEST) {
347
+ return new Manifest_1.Manifest(doc, shouldAcquire, shareSync);
348
+ }
349
+ else if (kind === ContentKind_1.ContentKind.METRIC) {
350
+ return new models_1.Metric(doc, shouldAcquire, shareSync);
351
+ }
352
+ else if (kind === ContentKind_1.ContentKind.MEDIA) {
353
+ return new models_1.Media(doc, shouldAcquire, shareSync);
354
+ }
347
355
  else if (kind === ContentKind_1.ContentKind.PROFILE) {
348
356
  return new models_1.Profile(doc, shouldAcquire, shareSync);
349
357
  }
358
+ else if (kind === ContentKind_1.ContentKind.PROGRESS) {
359
+ return new models_1.Progress(doc, shouldAcquire, shareSync);
360
+ }
361
+ else if (kind === ContentKind_1.ContentKind.SESSION) {
362
+ return new Session_1.Session(doc, shouldAcquire, shareSync);
363
+ }
364
+ else if (kind === ContentKind_1.ContentKind.TERM) {
365
+ return new models_1.Term(doc, shouldAcquire, shareSync);
366
+ }
367
+ else if (kind === ContentKind_1.ContentKind.USER) {
368
+ return new User_1.User(doc, shouldAcquire, shareSync);
369
+ }
370
+ else if (kind === ContentKind_1.ContentKind.WORKSPACE) {
371
+ return new models_1.Workspace(doc, shouldAcquire, shareSync);
372
+ }
350
373
  throw new Error(`can't wrap content ${kind}/${doc.id}`);
351
374
  }
352
375
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shaxpir/duiduidui-models",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/shaxpir/duiduidui-models"