Created
November 9, 2021 22:53
-
-
Save dnys1/a756eafa7dbfdd38d6b61396274ea144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model {} | |
class ConflictData<M extends Model> { | |
final M local; | |
final M remote; | |
const ConflictData(this.local, this.remote); | |
} | |
class ModelA extends Model {} | |
class ModelB extends Model {} | |
typedef ConflictHandler<M extends Model> = M Function(ConflictData<M>); | |
final Map<Type, ConflictHandler> handlers = {}; | |
void registerConflictHandler<M extends Model>(ConflictHandler<M> handler) { | |
handlers[M] = (ConflictData<Model> data) { | |
return handler(ConflictData<M>(data.local as M, data.remote as M)); | |
}; | |
} | |
void callConflictHandler(Type type, ConflictData data) { | |
handlers[type]!(data); | |
} | |
void main() { | |
registerConflictHandler((ConflictData<ModelA> conflictDataA) { | |
print('Conflicting A values'); | |
return conflictDataA.local; | |
}); | |
registerConflictHandler((ConflictData<ModelB> conflictDataB) { | |
print('Conflicting B values'); | |
return conflictDataB.remote; | |
}); | |
callConflictHandler(ModelA, ConflictData<ModelA>(ModelA(), ModelA())); | |
callConflictHandler(ModelB, ConflictData<ModelB>(ModelB(), ModelB())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment