Михаил Капелько 1年前
父节点
当前提交
37b4955bf4
共有 7 个文件被更改,包括 76 次插入87 次删除
  1. +4
    -6
      Modules/BusX/Bus/src/Bus.Processor.swift
  2. +56
    -65
      Modules/BusX/Bus/src/Bus.swift
  3. +2
    -2
      Modules/BusX/BusUI/src/BusUI.Button.swift
  4. +4
    -4
      Modules/BusX/BusUI/src/BusUI.TextField.swift
  5. +2
    -2
      Modules/BusX/BusUI/src/BusUI.Value.swift
  6. +4
    -4
      Modules/MeetupIdX/src/MeetupId.Debounce.swift
  7. +4
    -4
      Modules/MeetupIdX/src/MeetupId.Delay.swift

+ 4
- 6
Modules/BusX/Bus/src/Bus.Processor.swift 查看文件

@@ -7,19 +7,17 @@ public extension Bus {
public init( public init(
_ handler: @escaping ((Src) -> Dst?), _ handler: @escaping ((Src) -> Dst?),
_ keyIn: String, _ keyIn: String,
_ keyOut: String, _ keyOut: String
opt: [Option] = []
) { ) {
Bus.process([keyIn], keyOut, handler, opt: opt, sub: &subscriptions) Bus.processSync([keyIn], keyOut, handler, &subscriptions)
} }


public init( public init(
_ handler: @escaping ((Src) -> Dst?), _ handler: @escaping ((Src) -> Dst?),
_ keysIn: Set<String>, _ keysIn: Set<String>,
_ keyOut: String, _ keyOut: String
opt: [Option] = []
) { ) {
Bus.process(keysIn, keyOut, handler, opt: opt, sub: &subscriptions) Bus.processSync(keysIn, keyOut, handler, &subscriptions)
} }
} }
} }

+ 56
- 65
Modules/BusX/Bus/src/Bus.swift 查看文件

@@ -10,92 +10,83 @@ public enum Bus {
} }


public extension Bus { public extension Bus {
enum Option { /// Асинхронно обрабатывать входящие события из шины.
case async static func receiveAsync<T>(
_ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void),
_ subscriptions: inout [AnyCancellable]
) {
Self.events
.compactMap { convertKeyValue(keys, $0) }
.receive(on: DispatchQueue.main)
.sink { v in handler(v.0, v.1) }
.store(in: &subscriptions)
} }
} /// Синхронно обрабатывать входящие события из шины.

static func receiveSync<T>(
public extension Bus {
static func receive<T>(
_ keys: Set<String>, _ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void), _ handler: @escaping ((String, T) -> Void),
opt: [Option] = [], _ subscriptions: inout [AnyCancellable]
sub subscriptions: inout [AnyCancellable]
) { ) {
let isAsync = opt.contains(.async) Self.events

.compactMap { convertKeyValue(keys, $0) }
// Async. .sink { v in handler(v.0, v.1) }
if isAsync { .store(in: &subscriptions)
Self.events
.compactMap { convertKeyValue(keys, $0) }
.receive(on: DispatchQueue.main)
.sink { v in handler(v.0, v.1) }
.store(in: &subscriptions)
}

// Async.
if !isAsync {
Self.events
.compactMap { convertKeyValue(keys, $0) }
.sink { v in handler(v.0, v.1) }
.store(in: &subscriptions)
}
} }


static func send<T>( /// Асинхронно отправлять события из узла в шину.
static func sendAsync<T>(
_ key: String, _ key: String,
_ node: AnyPublisher<T, Never>, _ node: AnyPublisher<T, Never>,
opt: [Option] = [], _ subscriptions: inout [AnyCancellable]
sub subscriptions: inout [AnyCancellable]
) { ) {
let isAsync = opt.contains(.async) node

.receive(on: DispatchQueue.main)
// Async. .sink { v in Self.e.send((key, v)) }
if isAsync { .store(in: &subscriptions)
node }
.receive(on: DispatchQueue.main) /// Синхронно отправлять события из узла в шину.
.sink { v in Self.e.send((key, v)) } static func sendSync<T>(
.store(in: &subscriptions) _ key: String,
} _ node: AnyPublisher<T, Never>,

_ subscriptions: inout [AnyCancellable]
// Sync. ) {
if !isAsync { node
node .sink { v in Self.e.send((key, v)) }
.sink { v in Self.e.send((key, v)) } .store(in: &subscriptions)
.store(in: &subscriptions)
}
} }
/// Синхронно отправить событие в шину один раз.
static func send(_ key: String, _ value: Any) { static func send(_ key: String, _ value: Any) {
Self.e.send((key, value)) Self.e.send((key, value))
} }
} }


public extension Bus { public extension Bus {
static func process<Src, Dst>( static func processAsync<Src, Dst>(
_ keysIn: Set<String>, _ keysIn: Set<String>,
_ keyOut: String, _ keyOut: String,
_ handler: @escaping ((Src) -> Dst?), _ handler: @escaping ((Src) -> Dst?),
opt: [Option] = [], _ subscriptions: inout [AnyCancellable]
sub subscriptions: inout [AnyCancellable]
) { ) {
let isAsync = opt.contains(.async) Self.events

.compactMap { processKeysValue($0, keysIn, handler) }
// Async. .receive(on: DispatchQueue.main)
if isAsync { .sink { vOut in Self.e.send((keyOut, vOut)) }
Self.events .store(in: &subscriptions)
.compactMap { processKeysValue($0, keysIn, handler) } }
.receive(on: DispatchQueue.main)
.sink { vOut in Self.e.send((keyOut, vOut)) }
.store(in: &subscriptions)
}


// Sync. static func processSync<Src, Dst>(
if !isAsync { _ keysIn: Set<String>,
Self.events _ keyOut: String,
.compactMap { processKeysValue($0, keysIn, handler) } _ handler: @escaping ((Src) -> Dst?),
.sink { vOut in Self.e.send((keyOut, vOut)) } _ subscriptions: inout [AnyCancellable]
.store(in: &subscriptions) ) {
} Self.events
.compactMap { processKeysValue($0, keysIn, handler) }
.sink { vOut in Self.e.send((keyOut, vOut)) }
.store(in: &subscriptions)
} }
} }

+ 2
- 2
Modules/BusX/BusUI/src/BusUI.Button.swift 查看文件

@@ -6,10 +6,10 @@ extension BusUI {
var subscriptions = [AnyCancellable]() var subscriptions = [AnyCancellable]()
public init(_ key: String) { public init(_ key: String) {
Bus.send( Bus.sendSync(
key, key,
v.map { true }.eraseToAnyPublisher(), v.map { true }.eraseToAnyPublisher(),
sub: &subscriptions &subscriptions
) )
} }
} }


+ 4
- 4
Modules/BusX/BusUI/src/BusUI.TextField.swift 查看文件

@@ -10,19 +10,19 @@ extension BusUI {
_ textApp: String, _ textApp: String,
_ textUI: String _ textUI: String
) { ) {
Bus.send( Bus.sendSync(
textUI, textUI,
$v $v
.removeDuplicates() .removeDuplicates()
.compactMap(onlyUIText) .compactMap(onlyUIText)
.eraseToAnyPublisher(), .eraseToAnyPublisher(),
sub: &subscriptions &subscriptions
) )
Bus.receive( Bus.receiveSync(
[textApp], [textApp],
{ [weak self] (_, v: String) in self?.v = "a:\(v)" }, { [weak self] (_, v: String) in self?.v = "a:\(v)" },
sub: &subscriptions &subscriptions
) )
} }
} }


+ 2
- 2
Modules/BusX/BusUI/src/BusUI.Value.swift 查看文件

@@ -10,10 +10,10 @@ extension BusUI {
_ defaultValue: T _ defaultValue: T
) { ) {
v = defaultValue v = defaultValue
Bus.receive( Bus.receiveSync(
[key], [key],
{ [weak self] (_, v: T) in self?.v = v }, { [weak self] (_, v: T) in self?.v = v },
sub: &subscriptions &subscriptions
) )
} }
} }


+ 4
- 4
Modules/MeetupIdX/src/MeetupId.Debounce.swift 查看文件

@@ -12,19 +12,19 @@ extension MeetupId {
_ src: String, _ src: String,
_ dst: String _ dst: String
) { ) {
Bus.receive( Bus.receiveSync(
[src], [src],
{ [weak self] _, v in self?.v.send(v) }, { [weak self] _, v in self?.v.send(v) },
sub: &subscriptions &subscriptions
) )


Bus.send( Bus.sendSync(
dst, dst,
v v
.debounce(for: .seconds(sec), scheduler: DispatchQueue.main) .debounce(for: .seconds(sec), scheduler: DispatchQueue.main)
.compactMap { (v: Src) in handler(v) } .compactMap { (v: Src) in handler(v) }
.eraseToAnyPublisher(), .eraseToAnyPublisher(),
sub: &subscriptions &subscriptions
) )
} }
} }


+ 4
- 4
Modules/MeetupIdX/src/MeetupId.Delay.swift 查看文件

@@ -12,19 +12,19 @@ extension MeetupId {
_ src: String, _ src: String,
_ dst: String _ dst: String
) { ) {
Bus.receive( Bus.receiveSync(
[src], [src],
{ [weak self] _, v in self?.v.send(v) }, { [weak self] _, v in self?.v.send(v) },
sub: &subscriptions &subscriptions
) )


Bus.send( Bus.sendSync(
dst, dst,
v v
.delay(for: .seconds(sec), scheduler: DispatchQueue.main) .delay(for: .seconds(sec), scheduler: DispatchQueue.main)
.compactMap { (v: Src) in handler(v) } .compactMap { (v: Src) in handler(v) }
.eraseToAnyPublisher(), .eraseToAnyPublisher(),
sub: &subscriptions &subscriptions
) )
} }
} }


正在加载...
取消
保存