| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2024-09-03 20:37:36 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2024-09-03 20:37:36 UTC |
| parent | 21d29d44d50e68f7b749502cb30e360eebf001f0 |
| snikket/persistence/Custom.hx | +148 | -0 |
diff --git a/snikket/persistence/Custom.hx b/snikket/persistence/Custom.hx new file mode 100644 index 0000000..1448bfb --- /dev/null +++ b/snikket/persistence/Custom.hx @@ -0,0 +1,148 @@ +package snikket.persistence; + +#if cpp +import HaxeCBridge; +#end +import haxe.io.BytesData; +import snikket.Caps; +import snikket.Chat; +import snikket.Message; + +@:expose +#if cpp +@:build(HaxeCBridge.expose()) +@:build(HaxeSwiftBridge.expose()) +#end +class Custom implements Persistence { + private final backing: Persistence; + private var _storeMessage: Null<(String, ChatMessage, Callback<ChatMessage>)->Bool> = null; + + /** + Create a persistence layer that wraps another with optional overrides + + @returns new persistence layer + **/ + public function new(backing: Persistence) { + this.backing = backing; + } + + @HaxeCBridge.noemit + public function lastId(accountId: String, chatId: Null<String>, callback:(Null<String>)->Void):Void { + backing.lastId(accountId, chatId, callback); + } + + @HaxeCBridge.noemit + public function storeChat(accountId: String, chat: Chat) { + backing.storeChat(accountId, chat); + } + + @HaxeCBridge.noemit + public function getChats(accountId: String, callback: (Array<SerializedChat>)->Void) { + backing.getChats(accountId, callback); + } + + /** + Override the storeMessage method of the underlying persistence layer + + @param f takes three arguments, the account ID, the ChatMessage to store, and the Callback to call when done + return false to pass control to the wrapped layer (do not call the Callback in this case) + **/ + public function overrideStoreMessage(f: (String, ChatMessage, Callback<ChatMessage>)->Bool) { + _storeMessage = f; + } + + @HaxeCBridge.noemit + public function storeMessage(accountId: String, message: ChatMessage, callback: (ChatMessage)->Void) { + if (_storeMessage == null || !_storeMessage(accountId, message, new Callback(callback))) { + backing.storeMessage(accountId, message, callback); + } + } + + @HaxeCBridge.noemit + public function getMessages(accountId: String, chatId: String, beforeId: Null<String>, beforeTime: Null<String>, callback: (Array<ChatMessage>)->Void) { + backing.getMessages(accountId, chatId, beforeId, beforeTime, callback); + } + + @HaxeCBridge.noemit + public function getChatsUnreadDetails(accountId: String, chats: Array<Chat>, callback: (Array<{ chatId: String, message: ChatMessage, unreadCount: Int }>)->Void) { + backing.getChatsUnreadDetails(accountId, chats, callback); + } + + @HaxeCBridge.noemit + public function storeReaction(accountId: String, update: ReactionUpdate, callback: (Null<ChatMessage>)->Void) { + backing.storeReaction(accountId, update, callback); + } + + @HaxeCBridge.noemit + public function updateMessageStatus(accountId: String, localId: String, status:MessageStatus, callback: (ChatMessage)->Void) { + backing.updateMessageStatus(accountId, localId, status, callback); + } + + @HaxeCBridge.noemit + public function getMediaUri(hashAlgorithm:String, hash:BytesData, callback: (Null<String>)->Void) { + backing.getMediaUri(hashAlgorithm, hash, callback); + } + + @HaxeCBridge.noemit + public function storeMedia(mime:String, bd:BytesData, callback: ()->Void) { + backing.storeMedia(mime, bd, callback); + } + + @HaxeCBridge.noemit + public function storeCaps(caps:Caps) { + backing.storeCaps(caps); + } + + @HaxeCBridge.noemit + public function getCaps(ver:String, callback: (Caps)->Void) { + backing.getCaps(ver, callback); + } + + @HaxeCBridge.noemit + public function storeLogin(login:String, clientId:String, displayName:String, token:Null<String>) { + backing.storeLogin(login, clientId, displayName, token); + } + + @HaxeCBridge.noemit + public function getLogin(login:String, callback:(Null<String>, Null<String>, Int, Null<String>)->Void) { + backing.getLogin(login, callback); + } + + @HaxeCBridge.noemit + public function storeStreamManagement(accountId:String, smId:String, outboundCount:Int, inboundCount:Int, outboundQueue:Array<String>) { + backing.storeStreamManagement(accountId, smId, outboundCount, inboundCount, outboundQueue); + } + + @HaxeCBridge.noemit + public function getStreamManagement(accountId:String, callback: (Null<String>, Int, Int, Array<String>)->Void) { + backing.getStreamManagement(accountId, callback); + } + + @HaxeCBridge.noemit + public function storeService(accountId:String, serviceId:String, name:Null<String>, node:Null<String>, caps:Caps) { + backing.storeService(accountId, serviceId, name, node, caps); + } + + @HaxeCBridge.noemit + public function findServicesWithFeature(accountId:String, feature:String, callback:(Array<{serviceId:String, name:Null<String>, node:Null<String>, caps: Caps}>)->Void) { + backing.findServicesWithFeature(accountId, feature, callback); + } +} + +@:expose +#if cpp +@:build(HaxeCBridge.expose()) +@:build(HaxeSwiftBridge.expose()) +#end +class Callback<T> { + private final f: T->Void; + + @:allow(snikket) + private function new(f: T->Void) { + this.f = f; + } + + public function call(v: Any) { + f(v); + } +}